package com.haitun.risk.govern.web;


import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.haitun.risk.govern.commons.BaseConsts;
import com.haitun.risk.govern.model.RiskOrdersInfo;
import com.haitun.risk.govern.service.ReceiveInfoService;
import com.haitun.risk.govern.vo.ErpOrdersInfoRequestVO;
import com.haitun.risk.govern.vo.ResponseInfoVO;
/**
 * @Description: 关务风险评级管控
 * @author: rbkevin@qq.com 
 * @version: 2017年2月25日 上午8:55:47
 */
@RestController
public class RiskEvaluateController {

	private static Logger logger = LoggerFactory.getLogger(RiskEvaluateController.class);
	@Autowired
	ReceiveInfoService receiveInfoService;
	
	@RequestMapping(value="/checkOrder",method=RequestMethod.POST)
	public ResponseInfoVO checkOrder(@RequestBody ErpOrdersInfoRequestVO erpOrdersInfoRequestVO){
		ResponseInfoVO responseInfoVO=new ResponseInfoVO();
		String checkRequestStr=checkParameter(erpOrdersInfoRequestVO);
		if(StringUtils.isBlank(checkRequestStr)){
			responseInfoVO=receiveInfoService.checkRiskRank(erpOrdersInfoRequestVO);
		}else{
			responseInfoVO.setRetCode(BaseConsts.RETCODE_ERROR);
			responseInfoVO.setErrorMsg(checkRequestStr);
		}
		return responseInfoVO;
	}
	
	private String checkParameter(ErpOrdersInfoRequestVO erpOrdersInfoRequestVO){
		String rtnStr="";
		if(erpOrdersInfoRequestVO!=null&&erpOrdersInfoRequestVO.getRiskOrdersInfo()!=null){
			RiskOrdersInfo riskOrdersInfo=erpOrdersInfoRequestVO.getRiskOrdersInfo();
			if(StringUtils.isBlank(riskOrdersInfo.getBusinessType())){
				rtnStr="请求参数检测失败，业务类型为空！";
				logger.error(rtnStr);
				return rtnStr;
			}
			if(riskOrdersInfo.getErpOrderId()==null||riskOrdersInfo.getErpOrderId()==0){
				rtnStr="请求参数检测失败，ERP订单内单号为空！";
				logger.error(rtnStr);
				return rtnStr;
			}
			if(StringUtils.isBlank(riskOrdersInfo.getErpOrdersSn())){
				rtnStr="请求参数检测失败，ERP订单内单号为空！";
				logger.error(rtnStr);
				return rtnStr;
			}
			if(StringUtils.isBlank(riskOrdersInfo.getBuyerName())){
				rtnStr="请求参数检测失败，买家名称为空！";
				logger.error(rtnStr);
				return rtnStr;
			}
			if(StringUtils.isBlank(riskOrdersInfo.getBuyerPhone())){
				rtnStr="请求参数检测失败，买家电话为空！";
				logger.error(rtnStr);
				return rtnStr;
			}
			if(StringUtils.isBlank(riskOrdersInfo.getBuyerAddress1())){
				rtnStr="请求参数检测失败，买家地址1为空！";
				logger.error(rtnStr);
				return rtnStr;
			}
			if(StringUtils.isBlank(riskOrdersInfo.getBuyerCity())){
				rtnStr="请求参数检测失败，买家城市为空！";
				logger.error(rtnStr);
				return rtnStr;
			}
			if(StringUtils.isBlank(riskOrdersInfo.getBuyerState())){
				rtnStr="请求参数检测失败，买家省为空！";
				logger.error(rtnStr);
				return rtnStr;
			}
			if(StringUtils.isBlank(riskOrdersInfo.getBuyerZone())){
				rtnStr="请求参数检测失败，地区为空！";
				logger.error(rtnStr);
				return rtnStr;
			}
			if(StringUtils.isBlank(riskOrdersInfo.getBuyerCountry())){
				rtnStr="请求参数检测失败，买家国家为空！";
				logger.error(rtnStr);
				return rtnStr;
			}
			if(riskOrdersInfo.getOrdersTotal()==null||riskOrdersInfo.getOrdersTotal()==0){
				rtnStr="请求参数检测失败，订单总额为空！";
				logger.error(rtnStr);
				return rtnStr;
			}
		}else{
			rtnStr="请求参数检测失败,没接收到请求参数！";
		}
		return rtnStr;
	}
	
	/** @Description: 根据 身份证,电话,地址 维度更新累计使用额度
	 * @author: dave
	 * @param erpOrdersInfoRequestVO
	 * @return
	 * @version: 2017年2月27日 上午9:46:33
	*/
	@RequestMapping(value="/updateAmount",method=RequestMethod.POST)
	public ResponseInfoVO updateUserAmount(@RequestBody ErpOrdersInfoRequestVO erpOrdersInfoRequestVO){
		ResponseInfoVO responseInfoVO = new ResponseInfoVO();
		
		RiskOrdersInfo riskOrdersInfo = erpOrdersInfoRequestVO.getRiskOrdersInfo();
		if(riskOrdersInfo == null){
			logger.error("updateUserAmount_riskOrdersInfo null");
			responseInfoVO.setRetCode(1);
			responseInfoVO.setErrorMsg("parameter error");
			return responseInfoVO;
		}
		logger.info("updateUserAmount address:"+riskOrdersInfo.getAddress()+",phone:"+riskOrdersInfo.getBuyerPhone()+",certificate:"+riskOrdersInfo.getCertificate());
		
		//身份证,电话号,地址不能为空
		if(StringUtils.isBlank(riskOrdersInfo.getCertificate()) || StringUtils.isBlank(riskOrdersInfo.getBuyerPhone()) ||
				StringUtils.isBlank(riskOrdersInfo.getAddress())){
			responseInfoVO.setRetCode(1);
			responseInfoVO.setErrorMsg("parameter error");
			return responseInfoVO;
		}
		if(riskOrdersInfo.getCertificate().length()!=18 || riskOrdersInfo.getAddress().length()>128 || 
				riskOrdersInfo.getBuyerPhone().length()>32){
			responseInfoVO.setRetCode(1);
			responseInfoVO.setErrorMsg("paramter length too long");
			return responseInfoVO;
		}
		
		try {
			receiveInfoService.updateUserAmount(riskOrdersInfo);
		} catch (Exception e) {
			logger.error("updateUserAmount erropr:" + e.getMessage()+"/n" + e.getStackTrace());
			
			responseInfoVO.setRetCode(1);
			responseInfoVO.setErrorMsg("system error");
		}
		responseInfoVO.setRetCode(0);
		responseInfoVO.setErrorMsg("successful operation");
		return responseInfoVO;
	}
}
